package com.mz.ozy.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mz.ozy.R;
import com.mz.ozy.adapter.VipUpListAdapter;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.EventBean;
import com.mz.ozy.bean.VipUpBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.presenter.VipUpPresenter;
import com.mz.ozy.mvp.view.VipUpContract;
import com.mz.ozy.utils.PayManager;
import com.mz.ozy.utils.SPUtils;
import com.mz.ozy.utils.glideUtils.GlideUtils;
import com.mz.ozy.widget.dialog.PaySelectDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author lifeStyle on 2020/4/7 0007.
 */
public class UpVipActivity extends BaseActivity<VipUpContract.View, VipUpPresenter> implements VipUpContract.View, View.OnClickListener {

    @BindView(R.id.image_vip)
    ImageView imageVip;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.nestedScrollView)
    NestedScrollView nestedScrollView;

    private VipUpListAdapter mAdapter;
    private List<VipUpBean.VipListBean> mBeanList = new ArrayList<>();
    private VipUpBean.BannerListBean vipUpBean;
    private boolean isFirstLoad = true;
    private int payType;
    private PayManager mPayManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_up_vip);
        ButterKnife.bind(this);
        initTitle2();
        EventBus.getDefault().register(this);

        ll_top.setBackgroundColor(getResources().getColor(android.R.color.transparent));
        titleBar.setBackgroundColor(getResources().getColor(android.R.color.transparent));
        tvBack.setImageResource(R.mipmap.white_back_icon);
        tvCenterTitle.setTextColor(getResources().getColor(R.color.white));
        rightTextView.setTextColor(getResources().getColor(R.color.white));
        tvCenterTitle.setText("欧卓雅会员");
        rightTextView.setText("升级记录");
        rightTextView.setOnClickListener(this);

        recyclerview.setNestedScrollingEnabled(false);
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new VipUpListAdapter(mBeanList);
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        recyclerview.setAdapter(mAdapter);
        mPayManager = new PayManager(this);

        searchListByRefresh();


        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                VipUpBean.VipListBean itemsBean = mAdapter.getItem(position);
                switch (view.getId()) {
                    case R.id.tv_buy: {
                        showPaySelectDialog(itemsBean);

                        break;
                    }
                }
            }
        });
        nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
            @Override
            public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                Log.e("scrollY", scrollY + "");

                if (scrollY > 50) {
                    ll_top.setBackgroundColor(getResources().getColor(R.color.c_142d2a));
                    titleBar.setBackgroundColor(getResources().getColor(R.color.c_142d2a));
                } else {
                    ll_top.setBackgroundColor(getResources().getColor(android.R.color.transparent));
                    titleBar.setBackgroundColor(getResources().getColor(android.R.color.transparent));
                }


            }
        });
    }


    /**
     * 支付选择弹窗
     *
     * @param
     */
    private void showPaySelectDialog(VipUpBean.VipListBean data) {
        PaySelectDialog dialog = new PaySelectDialog(this, 0);
        dialog.setOnDialogListener(new PaySelectDialog.onDialogListener() {
            @Override
            public void onConfirm(int type, String payPwd) {
                if (type == 2) {
                    payType = 2;

                } else {
                    payType = 1;
                }
                mPresenter.onVipPay(UpVipActivity.this, String.valueOf(data.getId()), String.valueOf(payType));
            }

            @Override
            public void onCancel() {

            }
        });
        dialog.setData(data.getPrice(), "");
        dialog.toggleDialog();
    }


    private void searchListByRefresh() {
        mPresenter.getVipInfo(this);
    }


    @Override
    public void getVipInfoSuccess(VipUpBean bean) {
        if (isFirstLoad) {
            final VipUpBean.BannerListBean banner = bean.getBanner();
            if (banner != null) {
                vipUpBean = banner;
                GlideUtils.loadRoundImg(this, imageVip, banner.getImgPath(), 10);
            }
        }
        isFirstLoad = false;
        mBeanList.clear();
        mBeanList.addAll(bean.getList());
        Collections.reverse(mBeanList);
        mAdapter.notifyDataSetChanged();
        if (mBeanList.size() == 0) {
            llNoData.setVisibility(View.VISIBLE);
        } else {
            llNoData.setVisibility(View.GONE);
        }
    }

    @Override
    public void onVipPaySuccess(JSONObject json) {
        if (payType == 2) {  //微信支付
            mPayManager.handleWxPay(json);
        } else {  //支付宝支付
            mPayManager.handleAliPay(json.optString("zfb"));
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(EventBean event) {
        if ((event.getCode() == Constants.EventCode.WX_PAY_SUCCESS || event.getCode() == Constants.EventCode.ALIPAY_PAY_SUCCESS)) {
            Constants.isVip = true;
            SPUtils.getInstance(this).putString(Constants.PREE.IS_VIP, "1");
            startActivity(new Intent(this, VipUpgradeRecordActivity.class));
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onClick(View view) {
        startActivity(new Intent(UpVipActivity.this, VipUpgradeRecordActivity.class));

    }
}
